/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contable.presentacion;

import contable.aplicacion.FactoriaSQL;
import contable.dominio.Empleado;
import java.awt.Color;

/**
 *
 * @author jaime
 */
public class IngresarEmpleado extends javax.swing.JFrame {

    private javax.swing.JFrame parent;
    private Empleado empleado;
    private FactoriaSQL fsql;
    private boolean ingresar;

    /**
     * Creates new form IngresarEmpleado
     */
    public IngresarEmpleado(javax.swing.JFrame parent) {
        setLocationRelativeTo(parent);
        initComponents();
        this.parent = parent;
        this.parent.setVisible(false);
        this.fsql = FactoriaSQL.getINSTANCIA();
        this.ingresar = true;
        this.empleado = new Empleado();
    }

    /**
     * Actualizar los datos de un empleado
     *
     * @param parent La ventana que llama a está
     * @param empleado Los datos del empleado a modificar
     */
    public IngresarEmpleado(javax.swing.JFrame parent, Empleado empleado) {
        setLocationRelativeTo(parent);
        initComponents();
        setVisible(true);
        this.parent = parent;
        this.fsql = FactoriaSQL.getINSTANCIA();
        this.empleado = empleado;
        btnIngresar.setText("Actualizar");
        setTitle("Actualizar los datos de un empleado");
        btnLimpiar.setEnabled(false);
        ftfDUI.setEnabled(false);
        ingresar = false;
        cargarDatos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNombres = new javax.swing.JLabel();
        txtNombres = new javax.swing.JTextField();
        lblApellidos = new javax.swing.JLabel();
        txtApellidos = new javax.swing.JTextField();
        lblFechaNac = new javax.swing.JLabel();
        dtcFechaNac = new com.toedter.calendar.JDateChooser();
        lblSexo = new javax.swing.JLabel();
        cmbSexo = new javax.swing.JComboBox();
        lblDUI = new javax.swing.JLabel();
        ftfDUI = new javax.swing.JFormattedTextField();
        lblNIT = new javax.swing.JLabel();
        ftfNIT = new javax.swing.JFormattedTextField();
        lblISSS = new javax.swing.JLabel();
        ftfISSS = new javax.swing.JFormattedTextField();
        lblNUP = new javax.swing.JLabel();
        ftfNUP = new javax.swing.JFormattedTextField();
        lblCargo = new javax.swing.JLabel();
        cmbCargo = new javax.swing.JComboBox();
        lblSueldo = new javax.swing.JLabel();
        ftfSueldo = new javax.swing.JFormattedTextField();
        btnIngresar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ingresar un nuevo empleado");

        lblNombres.setText("Nombres:");
        lblNombres.setName("lblNombres"); // NOI18N

        txtNombres.setName("txtNombres"); // NOI18N

        lblApellidos.setText("Apellidos:");
        lblApellidos.setName("lblApellidos"); // NOI18N

        txtApellidos.setName("txtApellidos"); // NOI18N

        lblFechaNac.setText("Fecha de nacimiento:");
        lblFechaNac.setName("lblFechaNac"); // NOI18N

        dtcFechaNac.setDateFormatString("dd MMMM 'de' yyyy");
        dtcFechaNac.setName("dtcFechaNac"); // NOI18N

        lblSexo.setText("Sexo:");
        lblSexo.setName("lblSexo"); // NOI18N

        cmbSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hombre", "Mujer" }));
        cmbSexo.setName("cmbSexo"); // NOI18N

        lblDUI.setText("DUI:");
        lblDUI.setName("lblDUI"); // NOI18N

        try {
            ftfDUI.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("########-#")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ftfDUI.setName("ftfDUI"); // NOI18N

        lblNIT.setText("NIT:");
        lblNIT.setName("lblNIT"); // NOI18N

        try {
            ftfNIT.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-######-###-#")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ftfNIT.setName("ftfNIT"); // NOI18N

        lblISSS.setText("ISSS:");
        lblISSS.setName("lblISSS"); // NOI18N

        try {
            ftfISSS.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#########")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ftfISSS.setName("ftfISSS"); // NOI18N

        lblNUP.setText("NUP:");
        lblNUP.setName("lblNUP"); // NOI18N

        try {
            ftfNUP.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("############")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ftfNUP.setName("ftfNUP"); // NOI18N

        lblCargo.setText("Cargo:");
        lblCargo.setName("lblCargo"); // NOI18N

        cmbCargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vendedor", "Producción", "Secretaria", "Gerente", "Bodeguero", "Contador" }));
        cmbCargo.setName("cmbCargo"); // NOI18N

        lblSueldo.setText("Salario base:");
        lblSueldo.setName("lblSueldo"); // NOI18N

        ftfSueldo.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        ftfSueldo.setName("ftfSueldo"); // NOI18N

        btnIngresar.setText("Ingresar");
        btnIngresar.setName("btnIngresar"); // NOI18N
        btnIngresar.addMouseListener(formListener);

        btnCancelar.setText("Cancelar");
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addMouseListener(formListener);

        btnLimpiar.setText("Limpiar");
        btnLimpiar.setName("btnLimpiar"); // NOI18N
        btnLimpiar.addMouseListener(formListener);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNombres)
                            .addComponent(lblApellidos))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtApellidos, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                            .addComponent(txtNombres))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblFechaNac)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(dtcFechaNac, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblSexo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmbSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(lblDUI)
                                                .addComponent(lblNIT))
                                            .addComponent(lblISSS))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(ftfNIT, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                                            .addComponent(ftfDUI, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(ftfISSS)))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(lblSueldo)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(ftfSueldo))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(lblCargo)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(cmbCargo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(lblNUP)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(ftfNUP, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(31, 31, 31)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnIngresar)
                                    .addComponent(btnCancelar))))
                        .addGap(0, 12, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombres)
                    .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblApellidos)
                    .addComponent(txtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblFechaNac)
                    .addComponent(dtcFechaNac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblSexo)
                    .addComponent(cmbSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDUI)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ftfDUI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnIngresar)))
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNIT)
                            .addComponent(ftfNIT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblISSS)
                            .addComponent(ftfISSS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNUP)
                            .addComponent(ftfNUP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblCargo)
                            .addComponent(cmbCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(btnLimpiar)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSueldo)
                    .addComponent(ftfSueldo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.MouseListener {
        FormListener() {}
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            if (evt.getSource() == btnIngresar) {
                IngresarEmpleado.this.btnIngresarMouseClicked(evt);
            }
            else if (evt.getSource() == btnCancelar) {
                IngresarEmpleado.this.btnCancelarMouseClicked(evt);
            }
            else if (evt.getSource() == btnLimpiar) {
                IngresarEmpleado.this.btnLimpiarMouseClicked(evt);
            }
        }

        public void mouseEntered(java.awt.event.MouseEvent evt) {
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
        }

        public void mousePressed(java.awt.event.MouseEvent evt) {
        }

        public void mouseReleased(java.awt.event.MouseEvent evt) {
        }
    }// </editor-fold>//GEN-END:initComponents

    private void limpiarMensajes() {
        lblNombres.setForeground(Color.black);
        lblNombres.setToolTipText(null);
        lblApellidos.setForeground(Color.black);
        lblApellidos.setToolTipText(null);
        lblFechaNac.setForeground(Color.black);
        lblFechaNac.setToolTipText(null);
        lblSexo.setForeground(Color.black);
        lblSexo.setToolTipText(null);
        lblDUI.setForeground(Color.black);
        lblDUI.setToolTipText(null);
        lblNIT.setForeground(Color.black);
        lblNIT.setToolTipText(null);
        lblISSS.setForeground(Color.black);
        lblISSS.setToolTipText(null);
        lblNUP.setForeground(Color.black);
        lblNUP.setToolTipText(null);
        lblCargo.setForeground(Color.black);
        lblCargo.setToolTipText(null);
        lblSueldo.setForeground(Color.black);
        lblSueldo.setToolTipText(null);
    }

    private void cargarDatos() {
        txtNombres.setText(empleado.getNombres());
        txtApellidos.setText(empleado.getApellidos());
        dtcFechaNac.setDate(empleado.getFechaNacimiento());
        cmbSexo.setSelectedIndex(empleado.getSexo() == 'M' ? 0 : 1);
        ftfDUI.setText(empleado.getDUI());
        ftfNIT.setText(empleado.getNIT());
        ftfISSS.setText(empleado.getISSS());
        ftfNUP.setText(empleado.getNUP());
        cmbCargo.setSelectedIndex(empleado.getCargo());
        ftfSueldo.setText(String.valueOf(empleado.getSueldo()));
    }

    @Override
    public void dispose() {
        this.setVisible(false);
        parent.setVisible(true);
        super.dispose();
    }

    private void btnIngresarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnIngresarMouseClicked
        boolean error = false;
        limpiarMensajes();
        try {
            empleado.setNombres(txtNombres.getText());
        } catch (IllegalArgumentException iae) {
            lblNombres.setForeground(Color.red);
            lblNombres.setToolTipText(iae.getMessage());
            error = true;
        }
        try {
            empleado.setApellidos(txtApellidos.getText());
        } catch (IllegalArgumentException iae) {
            lblApellidos.setForeground(Color.red);
            lblApellidos.setToolTipText(iae.getMessage());
            error = true;
        }
        try {
            empleado.setFechaNacimiento(dtcFechaNac.getDate());
        } catch (IllegalArgumentException iae) {
            lblFechaNac.setForeground(Color.red);
            lblFechaNac.setToolTipText(iae.getMessage());
            error = true;
        }
        empleado.setSexo(cmbSexo.getSelectedIndex() == 0 ? 'M' : 'F');
        try {
            empleado.setDUI(ftfDUI.getText());
        } catch (IllegalArgumentException iae) {
            lblDUI.setForeground(Color.red);
            lblDUI.setToolTipText(iae.getMessage());
            error = true;
        }
        try {
            empleado.setNIT(ftfNIT.getText());
        } catch (IllegalArgumentException iae) {
            lblNIT.setForeground(Color.red);
            lblNIT.setToolTipText(iae.getMessage());
            error = true;
        }
        try {
            empleado.setISSS(ftfISSS.getText());
        } catch (IllegalArgumentException iae) {
            lblISSS.setForeground(Color.red);
            lblISSS.setToolTipText(iae.getMessage());
            error = true;
        }
        try {
            empleado.setNUP(ftfNUP.getText());
        } catch (IllegalArgumentException iae) {
            lblNUP.setForeground(Color.red);
            lblNUP.setToolTipText(iae.getMessage());
            error = true;
        }
        empleado.setCargo(cmbCargo.getSelectedIndex());
        try {
            empleado.setSueldo(Double.valueOf(ftfSueldo.getText()));
        } catch (IllegalArgumentException iae) {
            lblSueldo.setForeground(Color.red);
            lblSueldo.setToolTipText(iae.getMessage());
            error = true;
        }
        if (!error) {
            if (ingresar) {
                fsql.commit(empleado, empleado.getDUI());
            } else {
                fsql.commit(empleado);
            }
            limpiarMensajes();
            this.dispose();
        }
    }//GEN-LAST:event_btnIngresarMouseClicked

    private void btnLimpiarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLimpiarMouseClicked
        if (btnLimpiar.isEnabled()) {
            limpiarMensajes();
            txtNombres.setText(null);
            txtApellidos.setText(null);
            dtcFechaNac.setDate(null);
            cmbSexo.setSelectedIndex(0);
            ftfDUI.setText(null);
            ftfNIT.setText(null);
            ftfISSS.setText(null);
            ftfNUP.setText(null);
            cmbCargo.setSelectedIndex(0);
            ftfSueldo.setText(null);
        }
    }//GEN-LAST:event_btnLimpiarMouseClicked

    private void btnCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelarMouseClicked
        this.dispose();
    }//GEN-LAST:event_btnCancelarMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnIngresar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbCargo;
    private javax.swing.JComboBox cmbSexo;
    private com.toedter.calendar.JDateChooser dtcFechaNac;
    private javax.swing.JFormattedTextField ftfDUI;
    private javax.swing.JFormattedTextField ftfISSS;
    private javax.swing.JFormattedTextField ftfNIT;
    private javax.swing.JFormattedTextField ftfNUP;
    private javax.swing.JFormattedTextField ftfSueldo;
    private javax.swing.JLabel lblApellidos;
    private javax.swing.JLabel lblCargo;
    private javax.swing.JLabel lblDUI;
    private javax.swing.JLabel lblFechaNac;
    private javax.swing.JLabel lblISSS;
    private javax.swing.JLabel lblNIT;
    private javax.swing.JLabel lblNUP;
    private javax.swing.JLabel lblNombres;
    private javax.swing.JLabel lblSexo;
    private javax.swing.JLabel lblSueldo;
    private javax.swing.JTextField txtApellidos;
    private javax.swing.JTextField txtNombres;
    // End of variables declaration//GEN-END:variables
}
